const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const projectConfig = require('./project.config.js');
// const CleanWebpackPlugin = require('clean-webpack-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');

const webpackConfig = {
    entry: {},
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'devdist/'),
        publicPath: '/signin/devdist/'
    },
    mode: 'development',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: '../',
        open: true,
        host: '172.16.1.95',
        publicPath: './devdist/',
        watchContentBase: true,
        proxy: {
            '/api/circle': {
                // 线上
                // target: 'https://circle.qiushibaike.com',

                // 测试
                target: 'https://circle-test1.qiushibaike.com',
                changeOrigin: true,
                pathRewrite: { '^/api/circle': '' }
            },
            '/api/money': {
                // 线上
                // target: 'https://laisee.qiushibaike.com',

                // 测试
                target: 'https://laisee-test1.qiushibaike.com',
                changeOrigin: true,
                pathRewrite: { '^/api/money': '' }
            },
            '/api/mission': {
                // 线上
                // target: 'https://mission.qiushibaike.com',

                // 测试
                target: 'https://mission-test1.qiushibaike.com',
                changeOrigin: true,
                pathRewrite: { '^/api/mission': '' }
            }
        }
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                include: [path.resolve(__dirname, './'), /[\\/]node_modules[\\/].*antd/],
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'postcss-loader'
                    }
                ]
            },
            {
                test: /\.(html)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'html-loader'
                }
            },
            {
                test: /\.(png|jpg|gif)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[hash:5].[ext]',
                            outputPath: 'images/'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('development')
        }),
        // new CleanWebpackPlugin(['devdist']),
        new WriteFilePlugin()
    ],
    optimization: {
        splitChunks: {
            cacheGroups: {
                libs: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'libs/libs',
                    chunks: 'all'
                }
            }
        }
    }
};

projectConfig.pages.map((page) => {
    webpackConfig.entry[`pages/${page.template}/${page.template}`] = ['babel-polyfill', `./src/pages/${page.template}/index.js`];

    webpackConfig.plugins.push(new HtmlWebpackPlugin({
        title: page.title,
        filename: path.resolve(__dirname, `devdist/pages/${page.template}/index.html`),
        template: path.resolve(__dirname, `src/pages/${page.template}/index.html`),
        chunks: ['libs/libs', `pages/${page.template}/${page.template}`]
    }));
});

module.exports = webpackConfig;
